<template>
  <div class="container contract-review mb70">
    <p class="bread">
      <a href="#/index/tm-order-list" class="font-s-18">交易监控订单管理-监控师</a> >
      <a href="javascript:void(0);" class="font-s-16">详情</a>
    </p>
    <div class="bg-c-fff box-border" v-if="!this.errorType">
      <h3 class="tm-title">交易监控订单详情</h3>
      <step :stepData="stepData" :stepNum="stepNum" @stepRouter="stepRouter"></step>
    </div>
    <div class="surveillance-title-box">
      <div class="sides-choose-box" v-if="monitorType===1&&!errorType">
        <button type="button" class="blue-button" :class="{'active': currentSides===2}" @click="onSidesChange(2)">双方监控</button>
        <button type="button" class="blue-button" :class="{'active': currentSides===3}" @click="onSidesChange(3)">三方监控</button>
        <button type="button" class="blue-button" :class="{'active': currentSides===4}" @click="onSidesChange(4)">四方监控</button>
        <button type="button" class="blue-button" :class="{'active': currentSides===5}" @click="onSidesChange(5)">五方监控</button>
      </div>
      <div class="surveillance-title" v-else>交易监控流程图</div>
      <div class="btn-box">
        <button type="button" class="white-button" @click="message">添加监控事件</button>
        <button type="button" class="white-button" @click="onRemarksClick">{{remarksBtn}}</button>
      </div>
    </div>
    <div class="progress-box" v-show="companyNames.length!==0">
      <company-name-container :companyNames="companyNames" :isEdit="true" ref="companyNameContainer" :useInput="monitorType===1" :isError="errorType"></company-name-container>
      <div class="line" :class="{center: companyNames.length===2}" :style="{height: (events.length+0.5)*90+60+'px'}" v-show="events.length">
        <div class="arrow"></div>
      </div>
      <event-item v-for="(item,index) in events" :key="index" :pstate="item.state" :totalSides="companyNames.length" :side="item.side" :content="item.content" :files="item.files" :isEdit="getEditable(item.state)" :isCheck="!getEditable(item.state)" @edit="onEdit(item)" @delete="onDelete(item)" tradeType="trade" :serviceId="id" browerType="browser"></event-item>
      <div class="submit-box" :class="{'tc': companyNames.length===2}">
      </div>
      <p :class="{tc: companyNames.length===2}" class="end-text">交易监控结束</p>
      <div class="box bg-c-fff box-border auto tc button">
        <input type="button" value="保存" class="blue-button" @click="onSubmit">
        <input type="button" value="取消" class="white-button" @click="onCancel">
      </div>
    </div>
    <vodal :show="addEventShow" animation="zoom" :width="380" :height="310" @hide="addEventShow = false">
      <div class="vodal-xxbox">
        <h2>{{isEdit | title}}监控事件</h2>
        <div class="con">
          <div class="event-input-item">
            <label>被监控方:</label>
            <select v-model="inputSide">
              <option value="">请选择</option>
              <option v-for="(item,index) in companyNames" :value="index">{{index | side}}</option>
            </select>
          </div>
          <div class="event-input-item">
            <label for="numberInput">序号:</label>
            <input type="number" id="numberInput" v-model="inputNumber" step="1">
          </div>
          <div class="event-input-item">
            <label for="contentText">事件内容:</label>
            <textarea id="contentText" v-model="inputContent"></textarea>
          </div>
        </div>
      </div>
      <div class="tc auto vodal-box">
        <button class="blue-button" @click="onInputComfirm">确认</button>
        <button class="white-button" @click="addEventShow = false">取消</button>
      </div>
    </vodal>
    <vodal :show="remarksShow" animation="zoom" :width="360" :height="260" @hide="remarksShow = false">
      <div class="vodal-xxbox">
        <h2>{{remarksBtn}}信息</h2>
        <div class="con">
          <span>备注：</span>
          <textarea class="vt" v-model="remarks"></textarea>
        </div>
      </div>
      <div class="tc auto vodal-box">
        <button class="blue-button" type="button" @click="onRemarksComfirm">确认</button>
        <button class="white-button" type="button" @click="remarksShow = false">取消</button>
      </div>
    </vodal>
  </div>
</template>
<script type="text/javascript">
import Step from '../components/Step'
import CompanyNameContainer from '../components/CompanyNameContainer'
import EventItem from '../components/EventItem'
export default {
  components: {
    Step,
    CompanyNameContainer,
    EventItem
  },
  data() {
    return {
      id: '',
      stepNum: 2,
      stepData: [{
        name: '查看资质审查报告'
      }, {
        name: '审查合同'
      }, {
        name: '绘制流程图'
      }, {
        name: '绘制完成'
      }],
      remarksBtn: '添加备注',
      remarksShow: false,
      remarks: '',
      companyNames: [],
      nameOptions: [],
      events: [],
      addEventShow: false,
      isEdit: false,
      inputSide: '',
      inputNumber: '',
      inputContent: '',
      currentItem: null,
      monitorType: 0,
      currentSides: 2,
      errorType: false,
      lastIndex: 0
    }
  },
  mounted() {
    this.id = this.$route.query.id
    this.getData()
    if (this.$route.query.type) {
      this.errorType = true
    }
  },
  methods: {
    getEditable(state) {
      if (!this.errorType) {
        return true
      } else {
        if (state === 1) {
          return true
        } else {
          return false
        }
      }
    },
    onSidesChange(n) {
      if (this.currentSides !== n) {
        this.currentSides = n
        this.companyNames = []
        let i = 0
        for (i = 0; i < n; i++) {
          this.companyNames.push({})
        }
        this.events = []
      }
    },
    onSubmit() {
      let names = this.$refs.companyNameContainer.names
      let arr = []
      let i = 0
      let l = this.companyNames.length
      for (i = 0; i < l; i++) {
        if (names[i]) {
          arr.push(names[i])
        } else {
          let side = ''
          if (i === 0) {
            side = '甲方'
          }
          if (i === 1) {
            side = '乙方'
          }
          if (i === 2) {
            side = '丙方'
          }
          if (i === 3) {
            side = '丁方'
          }
          if (i === 4) {
            side = '戊方'
          }
          this.$toasted.show('请选择' + side + '的公司名称')
          return
        }
      }
      if (!this.events.length) {
        this.$toasted.show('请添加监控事件')
        return
      }
      this.$http.post('/transactionMonitorSer/monitor/mappingSer', {
        id: this.id,
        events: JSON.stringify(this.events),
        members: arr.join(','),
        remark: this.remarks
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.$toasted.show('提交成功！')
          if (this.errorType) {
            window.history.back()
          } else {
            this.$router.push('/tm-transaction-draw-finish?id=' + this.$route.query.id)
          }
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    getData() {
      this.$http.post('/transactionMonitorSer/sys/read/detail', {
        id: this.id
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.monitorType = data.data.data.monitorType
          this.status = data.data.data.status
          if (this.monitorType === 1 && !this.errorType) {
            this.companyNames = [{}, {}]
          } else {
            data.data.data.memberList.forEach(member => {
              this.companyNames.push({
                name: member.memberName,
                id: member.id
              })
            })
          }
          if (this.errorType) {
            data.data.data.eventList.forEach(item => {
              let event = {}
              event.content = item.content
              event.side = item.memberOrder
              event.state = item.status
              event.files = item.fileList || []
              event.index = item.order
              event.id = item.id
              this.events.push(event)
              if (event.state !== 1) {
                this.lastIndex = event.index
              }
            })
            if (data.data.data.remark) {
              this.remark = data.data.data.remark
            }
          }
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    onEdit(item) {
      this.currentItem = item
      this.isEdit = true
      this.inputSide = item.side
      this.inputNumber = item.index
      this.inputContent = item.content
      this.addEventShow = true
    },
    onDelete(item) {
      this.events.forEach((i, index) => {
        if (i === item) {
          this.events.splice(index, 1)
        }
      })
    },
    onInputComfirm() {
      if (this.inputSide === '') {
        this.$toasted.show('请选择被监控方')
        return
      }
      let number = parseInt(this.inputNumber)
      if (this.inputNumber === '' || isNaN(number)) {
        this.$toasted.show('请输入有效序号')
        return
      }
      let hasNumber = false
      this.events.forEach(item => {
        if (item.index === number) {
          hasNumber = true
        }
      })
      if (hasNumber && !this.isEdit) {
        this.$toasted.show('您输入的序号重复')
        return
      }
      if (this.inputContent === '') {
        this.$toasted.show('请输入事件内容')
        return
      }
      if (number <= this.lastIndex && this.errorType && !this.isEdit) {
        this.$toasted.show('请您输入的序号必须大于' + this.lastIndex)
        return
      }
      if (this.isEdit) {
        this.currentItem.side = parseInt(this.inputSide)
        this.currentItem.content = this.inputContent
        this.currentItem.index = number
      } else {
        let event = {
          side: parseInt(this.inputSide),
          files: [],
          content: this.inputContent,
          state: 1,
          index: number
        }
        this.events.push(event)
      }

      this.sortEvent()
      this.addEventShow = false
    },
    message() {
      this.addEventShow = true
      this.isEdit = false
      this.inputContent = ''
      this.inputNumber = this.events.length + 1
      this.inputSide = ''
    },
    onCancel() {
      window.history.back()
    },
    sortEvent() {
      this.events.sort(function(item1, item2) {
        if (item1.index > item2.index) {
          return true
        } else {
          false
        }
      })
    },
    onRemarksClick() {
      this.remarksShow = true
    },
    onRemarksComfirm() {
      if (this.remarks) {
        this.remarksBtn = '修改备注'
      }
      this.remarksShow = false
    },
    stepRouter(i, name) {
      console.log('aaa')
      if (i === 0 && this.status >= 2) {
        this.$router.push('/tm-transaction-aptitude?id=' + this.$route.query.id)
      }
      if (i === 1 && this.status >= 3) {
        this.$router.push('/tm-transaction-contract?id=' + this.$route.query.id)
      }
      if (i === 2 && this.status > 4) {
        // this.$router.push('/tm-transaction-draw?id=' + this.$route.query.id)
        this.$toasted.show('请查看绘制完成图')
      }
      if (i === 2 && this.status === 4) {
        this.$router.push('/tm-transaction-draw?id=' + this.$route.query.id)
      }
      if (i === 3 && this.status >= 5) {
        this.$router.push('/tm-transaction-draw-finish?id=' + this.$route.query.id)
      }
    }
  },
  filters: {
    title(v) {
      if (v) {
        return '编辑'
      } else {
        return '添加'
      }
    },
    side(v) {
      if (v === 0) {
        return '甲方'
      }
      if (v === 1) {
        return '乙方'
      }
      if (v === 2) {
        return '丙方'
      }
      if (v === 3) {
        return '丁方'
      }
      if (v === 4) {
        return '戊方'
      }
    }
  }
}

</script>
<style type="text/css" scoped>
.content {
  padding: 30px 0;
  margin-top: 20px;
}

.surveillance-title-box {
  position: relative;
  width: 100%;
  height: 40px;
  line-height: 40px;
  margin-top: 20px;
  border-bottom: 1px solid #ddd;
  background-color: #fff;
  text-align: center;
}

.surveillance-title {
  font-size: 14px;
  color: #282828;
}

.surveillance-title-box .btn-box {
  position: absolute;
  top: 0;
  right: 100px;
  font-size: 14px;
}

.surveillance-title-box .white-button {
  width: 100px;
  height: 30px;
  line-height: 30px;
  margin-left: 10px;
}

.progress-box {
  position: relative;
  width: 100%;
  background-color: #fff;
  overflow-x: auto;
  padding-bottom: 20px;
}

.line {
  position: absolute;
  width: 2px;
  top: 0;
  left: 65px;
  margin-top: 10px;
  background-color: #cfcfcf;
}

.line.center {
  left: 50%;
}

.line .arrow {
  position: absolute;
  bottom: -10px;
  left: -7px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 12px solid #cfcfcf;
}

.submit-box {
  width: 100%;
  padding: 40px;
}

.submit-box button {
  width: 120px;
  height: 35px;
}

h2 {
  background-color: #d8e7ff;
  font-size: 14px;
  font-weight: bold;
  line-height: 40px;
  text-indent: 40px;
}

.con {
  margin: 10px 20px;
  padding: 13px 0 0 13px;
}

textarea {
  border: 1px solid #ddd;
  width: 240px;
  padding: 6px 10px;
  line-height: 20px;
  height: 120px;
}

.button .white-button,
.button .blue-button {
  width: 245px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  margin: 40px 15px;
}

.vodal-box button {
  margin: 0 15px;
  width: 120px;
  height: 35px;
}

.event-input-item {
  min-height: 30px;
}

.event-input-item label {
  display: inline-block;
  width: 80px;
  vertical-align: top;
}

.event-input-item select {
  width: 238px;
  border: 1px solid #a5a5a5;
}

.event-input-item input {
  width: 238px;
  border: 1px solid #a5a5a5;
}

.end-text {
  padding: 0 30px;
}

.sides-choose-box {
  text-align: left;
}

.sides-choose-box button {
  float: left;
  height: 40px;
  border: none;
  padding: 0 20px;
}

.sides-choose-box button.active {
  background-color: #fff;
  color: #01378c;
}

</style>
